package com.itqf.controller;

import cn.dsna.util.images.ValidateCode;
import com.baomidou.mybatisplus.extension.api.R;
import com.itqf.aop.MyLog;
import com.itqf.dto.LoginUser;
import com.itqf.dto.TableData;
import com.itqf.dto.TableParam;
import com.itqf.entity.SysUser;
import com.itqf.service.SysUserService;
import com.itqf.utils.ShiroUtils;
import com.itqf.utils.SysConstant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/25
 * @Time: 上午10:27
 */
@RestController
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/captcha.jpg")
    public void captcha(HttpServletResponse response){
        try {
            ValidateCode validateCode = new ValidateCode(80,25,1,1);
            String text = validateCode.getCode();
            //存储到域对象中
            ShiroUtils.setAttribute(SysConstant.KAPTCHA,text);
            ImageIO.write(validateCode.getBuffImg(),"jpg",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/sys/login")
    public R  login(@RequestBody LoginUser user){

            String c = (String)ShiroUtils.getAttribute(SysConstant.KAPTCHA);
            //验证码
            if (!user.getCaptcha().equalsIgnoreCase(c)){
                return  R.failed("验证码错误");
            }

       // try {
            String p = new Md5Hash(user.getPassword(),user.getUsername(),1024).toString();
            System.out.println("加密后：-->"+p);
            //1,把用户名密码封装成UsernamePasswordToken
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),p);

            //记住我
            if (user.isRememberMe()){
                token.setRememberMe(true);
            }

            //2,得到subject对象
            Subject subject = SecurityUtils.getSubject();
            //3.登录
            subject.login(token);

            return R.ok(null);
//        } catch (AuthenticationException e) {
//            e.printStackTrace();
//        }


    }

    @MyLog(value = "用户信息",description = "展示该用户的信息")
    @RequestMapping("/sys/user/info")
    public   R userInfo(){
        SysUser sysUser = ShiroUtils.getCurrentUser();
        return R.ok(sysUser);
    }


    @RequestMapping("/sys/user/list")
    public TableData  findUserList(TableParam param){

        return  sysUserService.findUserList(param);
    }


    @RequestMapping("/sys/user/logout")
    public R  logout(){
        SecurityUtils.getSubject().logout();
        return R.ok(null);
    }
}
